﻿using Cache;
using Domain.Base;
using Domain.Entity;
using DtoFw;
using DtoFw.Base;
using DtoFw.Common.Dto;
using DtoFw.Common.Query;
using DtoFw.Query;
using DtoFw.Result;
using Repository;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Service
{
    public class MainOrganService
    {
        private readonly MainOrganRepository _organRepository;
        public MainOrganService(MainOrganRepository organRepository)
        {
            this._organRepository = organRepository;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<Pager<OrganResult>> Query(OrganQuery query)
        {
            var _pager = _organRepository.GetPager(query);
            Pager<OrganResult> pager = new Pager<OrganResult>();
            pager.Data = Util.AutoMapperExtension.MapTo<MainOrganEntity, OrganResult>(_pager.Data);
            pager.TotalCount = _pager.TotalCount;
            return BaseGenericResult<Pager<OrganResult>>.Succeed(pager);
        }

        public List<MainOrganEntity> QueryAll(OrganQuery query)
        {
            var organids = _organRepository.QueryOrgs(query.OrganId);
            var lst = _organRepository.Query(v => v.IsDeleted == false && organids.Contains(v.Id)).OrderBy(v => v.OrderBy).ToList();
            return lst;
        }
        public List<MainOrganEntity> QueryAll(DateTime? latest)
        {
            if (latest == null)
            {
                return _organRepository.Query(v => v.IsDeleted == false);
            }
            else
            {
                return _organRepository.Query(v => v.ModifyerTime >= latest.Value);
            }
        }
        /// <summary>
        /// 查询单位树状对象
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<OrganTreeDto> GetOrganTree(CommonQuery query)
        {
            return BaseGenericResult<OrganTreeDto>.Succeed(_organRepository.GetAllOrgan(query));
        }

        public MainOrganEntity Get(string corpCode)
        {
            return _organRepository.Query(v => v.Code == corpCode).FirstOrDefault();
        }

        /// <summary>
        /// 查询单个对象
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<OrganResult> Get(long OrganId)
        {
            var entity = _organRepository.Get(
                new BaseQuery(),
                x => x.Where(m => m.Id == OrganId));
            return BaseGenericResult<OrganResult>.Succeed(
                Util.AutoMapperExtension.MapTo<MainOrganEntity, OrganResult>(entity));
        }
        /// <summary>
        /// 新增机构
        /// </summary>
        /// <returns></returns>
        public BaseResult Post(PostOrgnQuery query)
        {
            var entity = Util.AutoMapperExtension.MapTo<PostOrgnQuery, MainOrganEntity>(query);
            entity.CreateTime = DateTime.Now;
            entity.Creator = query.UserName;
            entity.CreatorId = query.UserId;
            if (_organRepository.Insert(entity) > 0)
                return BaseResult.Succeed();
            return BaseResult.Failure("新增机构失败");
        }
        /// <summary>
        /// 修改机构
        /// </summary>
        /// <returns></returns>
        public BaseResult Put(PostOrgnEditQuery query)
        {
            var entity = _organRepository.Get(new BaseQuery() { Id = query.Id });
            entity = Util.AutoMapperExtension.MapTo<PostOrgnQuery, MainOrganEntity>(query, entity);
            entity.ModifyerTime = DateTime.Now;
            entity.Modifyer = query.UserName;
            entity.ModifyerId = query.UserId;
            if (_organRepository.Save(entity)?.Id > 0)
                return BaseResult.Succeed();
            return BaseResult.Failure("新增机构失败");
        }
        /// <summary>
        /// 删除机构
        /// </summary>
        /// <returns></returns>
        public BaseResult Delete(DeleteDto query)
        {
            if (query.Ids.Count < 1)
                return BaseResult.Failure("参数异常");
            var entity = _organRepository.Get(new BaseQuery() { Ids = query.Ids });
            if (_organRepository.Count(new BaseQuery(),
                   x => x.Where(m => m.SuperiorId == entity.Id)) > 0)
                return BaseResult.Failure("存在下级机构");
            if (_organRepository
                .DeleteBatch(new BaseOperate()
                {
                    Ids = query.Ids,
                    Modifyer = query.UserName,
                    ModifyerId = query.UserId
                }) > 0)
                return BaseResult.Succeed();
            return BaseResult.Failure("删除机构失败");
        }
    }
}
